﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.Common.Notify;
using DevExpress.XtraTreeList.Nodes;

namespace Power.UserInfo
{
    public partial class UserManager : DevExpress.XtraEditors.XtraForm
    {
        #region Property
        BLL.Sys_userBLL userBLL = new BLL.Sys_userBLL();
        private bool isAddUser = false;
        #endregion
        public UserManager()
        {
            InitializeComponent();
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            //initalize ui
            using (new DevExpress.Utils.WaitDialogForm("正在加载数据", "请等待..."))
            {
                LoadUserInfo();
            }
            InitalizeRole();
        }
        private void LoadUserInfo()
        {
            try
            {
                TreeListNode node;
                treeList1.BeginUnboundLoad();
                treeList1.ClearNodes();
                treeList1.Nodes.Clear();
                node = treeList1.AppendNode(new object[] { "Users", "Users", null, null }, null);
                node.StateImageIndex = 0;

                //get data from table                
                DataTable dtRule = userBLL.GetAllList().Tables[0];
                for (int i = 0; i < dtRule.Rows.Count; i++)
                {
                    node = treeList1.AppendNode(new object[] { dtRule.Rows[i]["user_id"], i, null, null }, 0);
                    node.StateImageIndex = 0;
                }
                treeList1.EndUnboundLoad();
                treeList1.Refresh();
                treeList1.ExpandAll();
                               
            }
            catch (Exception ex)
            {
                NotifyMessage.Show(ex);
            }
        }

        private void treeList1_Click(object sender, EventArgs e)
        {
            TreeListNode clickedNode = this.treeList1.FocusedNode;
            string disPlayText = clickedNode.GetDisplayText(0);
            if (disPlayText.ToUpper() != "USERS")
            {
                DisplayForm(disPlayText);
            }
        }
        private void DisplayForm(string userId)
        {
            try
            {
                if (userId != "")
                {
                    //get user info                    
                    Model.Sys_user mUser = userBLL.GetModel(userId);

                    //show message
                    txtUserID.Text = mUser.user_id;
                    txtUserName.Text = mUser.user_name;
                    txtRole.ItemIndex =int.Parse(mUser.user_role);
                    txtRemark.Text = mUser.remark;
                    txtPassword.Text = mUser.user_password;
                    txtConfrimPwd.Text = mUser.user_password;
                }
            }
            catch (Exception ex)
            {                
                NotifyMessage.Show(ex);
            }
        }

        private void InitalizeRole()
        {
            try
            {
                #region initalize datatable
                DataTable dtUser = new DataTable();

                DataColumn dcId = new DataColumn("id", typeof(string));
                DataColumn dcDesc = new DataColumn("desc", typeof(string));

                dtUser.Columns.Add(dcId);
                dtUser.Columns.Add(dcDesc);

                DataRow dtRow = dtUser.NewRow();
                dtRow["role"] = "1";
                dtRow["desc"] = "管理员";
                dtUser.Rows.Add(dtRow);

                dtRow = dtUser.NewRow();
                dtRow["role"] = "2";
                dtRow["desc"] = "工程师";
                dtUser.Rows.Add(dtRow);

                dtRow = dtUser.NewRow();
                dtRow["role"] = "3";
                dtRow["desc"] = "一般员工";
                dtUser.Rows.Add(dtRow);

                #endregion

                #region set data source
                txtRole.Properties.DataSource = dtUser;
                txtRole.Properties.DisplayMember = "desc";
                txtRole.Properties.ValueMember = "id";
                #endregion

            }
            catch (Exception ex)
            {
                NotifyMessage.Show(ex);
            }
        }
        private bool IsValidInput()
        {
            bool ret = false;
            if (string.IsNullOrEmpty(txtUserID.Text))
            {
                NotifyMessage.Show("Warning", "用户编码不能为空!!");
                return ret;
            }
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                NotifyMessage.Show("Warning", "用户名不能为空!!");
                return ret;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                NotifyMessage.Show("Warning", "密码不能为空!!");
                return ret;
            }
            if (string.IsNullOrEmpty(txtConfrimPwd.Text))
            {
                NotifyMessage.Show("Warning", "确认密码不能为空!!");
                return ret;
            }
            if(txtPassword.Text !=txtConfrimPwd.Text)
            {
                NotifyMessage.Show("Warning", "二次输入的密码不相同!!");
                return ret;
            }

            ret = true;
            return ret;
        }

        private bool SaveUser()
        {
            bool ret = false;
            try
            {
                if (isAddUser)
                {
                    //new one 
                }
                else
                {
                    //update
                }
                
                //refrash
                LoadUserInfo();

                return ret;
            }
            catch (Exception ex)
            {
                NotifyMessage.Show(ex);
                return false;
            }
        }
        
    }
}